﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IP2Server
{
    class UserStorage
    {
        public static List<User> allUsers = new List<User>();
        public enum UserGroup
        {
            ALL_USERS = 0, ALL_DOCTORS = 1, ALL_PATIENTS = 2
        }

        /// <summary>
        /// Method for adding a user to the 'allPatients' list
        /// This also updates the entire saving list.
        /// This might be a little inefficient, but it will not result in performance drains
        /// with lists containing< 1000 users.
        /// </summary>
        /// <param name="user">The User to add to the list</param>
        public static void AddUser(User user)
        {
            allUsers.Add(user);

            using (StreamWriter streamWriter = new StreamWriter("users.txt", true))
            {
                streamWriter.WriteLine(user.UserName + "\t" + user.Password + "\t" + user.AccessRights + "\t" + user.DoctorName);
            }
        }

        /// <summary>
        /// Method to validate if a users is allowed access.
        /// When a user is valid, the method will return a 'User' instance containing some of the users details.
        /// When a user is not valid, the method will return a empty 'User' instance.
        /// </summary>
        /// <param name="inlogData">A string[] containing the users username en password</param>
        /// <returns>An instance of the class 'User'</returns>
        public static User ValidateUserLogin(string[] inlogData)
        {
            string strUserName = inlogData[0];
            string strPassword = inlogData[1];

            foreach (User patient in allUsers)
            {
                if (patient.UserName == strUserName && patient.Password == strPassword)
                    return patient;
            }

            return null;
        }

        /// <summary>
        /// Method used to write all users to a (text) file.
        /// </summary>
        public static void SaveAllUsers()
        {
            //Gebruik een StreamWriter
            using (StreamWriter streamWriter = new StreamWriter("users.txt"))
            {
                //Tim, vergeet die doctorname niet godverdikkie
                foreach (User user in allUsers)
                    streamWriter.WriteLine(user.UserName + "\t" + user.Password + "\t" + user.AccessRights + "\t" + user.DoctorName);
            }
        }

        /// <summary>
        /// Method to open all users from a (text) file
        /// This will automatically write them to the list.
        /// </summary>
        public static void OpenAllUsers()
        {
            allUsers.Clear();

            if (!File.Exists("users.txt"))
                return;

            //Gebruik een StreamReader
            using (StreamReader streamReader = new StreamReader("users.txt"))
            {
                string inputLine;

                while ((inputLine = streamReader.ReadLine()) != null)
                {
                    try
                    {
                        //Splitten naar bruikbare informatie.
                        string[] splitString = inputLine.Split(null);

                        //Toevoegen.
                        //Ook hier weer die dokternaam
                        allUsers.Add(new User(splitString[0], splitString[1], splitString[2], splitString[3]));
                    }
                    catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); }
                }
            }
        }
    }
}
